package com.autumn.zero.authorization.services.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.autumn.mybatis.criterion.Query;
import com.autumn.util.AutoMapUtils;
import com.autumn.zero.authorization.entitys.roles.Role;
import com.autumn.zero.authorization.entitys.roles.querys.RoleByUserQuery;
import com.autumn.zero.authorization.entitys.users.User;
import com.autumn.zero.authorization.entitys.users.querys.UserByRoleQuery;
import com.autumn.zero.authorization.repositorys.roles.IRoleRepository;
import com.autumn.zero.authorization.repositorys.roles.querys.IRoleByUserQueryRepository;
import com.autumn.zero.authorization.repositorys.users.IUserRepository;
import com.autumn.zero.authorization.repositorys.users.querys.IUserByRoleQueryRepository;
import com.autumn.zero.authorization.services.IAuthorizationService;

/**
 * 默认授权
 * 
 * @author 杨昌国 2018-11-30 11:15:30
 */
public class AuthorizationServiceImpl extends AbstractAuthorizationService<User, IUserRepository, Role, IRoleRepository>
		implements IAuthorizationService {

	/**
	 * 
	 */
	public AuthorizationServiceImpl() {
		super(User.class, Role.class);
	}

	@Autowired
	private IUserRepository userRepository;

	@Autowired
	private IRoleRepository roleRepository;

	@Autowired
	private IRoleByUserQueryRepository roleUserQueryRepository;

	@Autowired
	private IUserByRoleQueryRepository userRoleQueryRepository;

	@Override
	protected final IUserRepository getUserRepository() {
		return this.userRepository;
	}

	@Override
	protected final IRoleRepository getRoleRepository() {
		return this.roleRepository;
	}

	@Override
	public List<Role> queryUserByRoles(long userId) {
		Query query = new Query(RoleByUserQuery.class);
		query.eq(RoleByUserQuery.FILED_USER_ID, userId).orderBy(RoleByUserQuery.FILED_SORT_ID)
				.orderBy(RoleByUserQuery.FILED_ID);
		List<RoleByUserQuery> queryRoles = roleUserQueryRepository.selectList(query.builderSection());
		return AutoMapUtils.mapList(queryRoles, Role.class);
	}

	@Override
	public List<User> queryRoleByUsers(long roleId) {
		Query query = new Query(UserByRoleQuery.class);
		query.eq(UserByRoleQuery.FILED_ROLE_ID, roleId).orderBy(UserByRoleQuery.FILED_ID);
		List<UserByRoleQuery> queryUoles = userRoleQueryRepository.selectList(query.builderSection());
		return AutoMapUtils.mapList(queryUoles, User.class);
	}

}
